//
//  ALPS_TRadioTopView.m
//  Alps
//
//  Created by dllo on 16/6/7.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ALPS_TRadioTopView.h"
#import "ALPS_TRadioTopCCCell.h"
#import "ALPS_TRadioUnderCCell.h"
#import "ALPS_TRadioModel.h"
@implementation ALPS_TRadioTopView

- (void)dealloc
{
    _topCollectionView.delegate = nil;
    _topCollectionView.dataSource = nil;
    _underCollectionView.dataSource = nil;
    _underCollectionView.delegate = nil;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //上方
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake(ScreeWidth / 4, 100);
        flowLayout.minimumLineSpacing = 0;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        self.topCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreeWidth, 100) collectionViewLayout:flowLayout];
        self.topCollectionView.dataSource = self;
        self.topCollectionView.delegate = self;
        self.topCollectionView.scrollEnabled = NO;
        self.topCollectionView.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.00];
        [self addSubview:_topCollectionView];
        [self.topCollectionView registerClass:[ALPS_TRadioTopCCCell class] forCellWithReuseIdentifier:@"cell"];
        
        
        
        //下方
        UICollectionViewFlowLayout *underFlowLayout = [[UICollectionViewFlowLayout alloc] init];
        underFlowLayout.itemSize = CGSizeMake(ScreeWidth / 4, 20);
        underFlowLayout.minimumLineSpacing = 5;
        underFlowLayout.minimumInteritemSpacing = 5;
        underFlowLayout.sectionInset = UIEdgeInsetsMake(0, 20, 0, 20);
        underFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        self.underCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100, ScreeWidth, 120) collectionViewLayout:underFlowLayout];
        self.underCollectionView.delegate = self;
        self.underCollectionView.dataSource = self;
        self.underCollectionView.scrollEnabled = NO;
        self.underCollectionView.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.00];
        [self addSubview:_underCollectionView];
        
        [self.underCollectionView registerClass:[ALPS_TRadioUnderCCell class] forCellWithReuseIdentifier:@"cell1"];
        
    }
    return self;
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView == self.topCollectionView) {
        return 4;
    }else
    {
        return self.dataSoucre.count;
    }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _topCollectionView) {
        
        ALPS_TRadioTopCCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        //辅助跳转到vc里
        cell.radioValue = ^(NSInteger num)
        {
            self.auxiliaryValue(num);
            
        };
        switch (indexPath.row) {
            case 0:
                cell.nameLabel.text = @"本地台";
                cell.pView.image = [UIImage imageNamed:@"benditai"];
                cell.number = 0;
                break;
            case 1:
                cell.nameLabel.text = @"国家台";
                cell.pView.image = [UIImage imageNamed:@"guojiatai"];
                cell.number = 1;
                break;
            case 2:
                cell.nameLabel.text = @"省市台";
                cell.pView.image = [UIImage imageNamed:@"shengshitai"];
                cell.number = 2;
                break;
            default:
                cell.nameLabel.text = @"网络台";
                cell.pView.image = [UIImage imageNamed:@"wangluotai"];
                cell.number = 3;
                break;
        }
        return cell;
    }else
    {
    ALPS_TRadioUnderCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell1" forIndexPath:indexPath];
        cell.model = [self.dataSoucre objectAtIndex:indexPath.row];
        return cell;
    }
    
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
   
    if (collectionView == self.underCollectionView) {
       __weak ALPS_TRadioModel *model = [self.dataSoucre objectAtIndex:indexPath.row];
        self.value(model.id, model.name);
        
    }
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
